<template>
	<view>
		<navbar name="记录血压"></navbar>
		<view class="weui-flex">
			<view>
				<view class="placeholder titleView">
					血压
				</view>
			</view>
			<view class="weui-flex__item">
				<view class="placeholder toolView" :class="label2 == '请选择血压'?'':'colorA4'"
					@click="handleTap('picker2')">
					<!-- {{value2.join(' ')}} -->
					{{label2}}
				</view>
			</view>
		</view>
		<u-line />
		<view class="weui-flex">
			<view>
				<view class="placeholder titleView">
					选择时间
				</view>
			</view>
			<view class="weui-flex__item">
				<view class="placeholder toolView" :class="value1==''?'':'colorA4'" @click="handleTap('picker1')">
					{{value1==''?label:value1.replace(/-/g,".")}}
				</view>
			</view>
		</view>
		<button class="relevance-btn" type="default" :disabled="disabled||label2 == '请选择血压'||value1==''?true:false"
			:class="label2 == '请选择血压'||value1==''?'disabledbg':''" @tap="addRecord(value2,value1)">保存</button>
		<!-- 日期选择 -->
	<!-- 	<lb-picker ref="picker1" v-model="value1" mode="dateSelector" @change="handleChange"
		  @confirm="handleConfirm"
			@cancel="handleCancel" start-date="2020-12-31"  :end-date="endTime">
		</lb-picker> -->
		<lb-picker ref="picker1" v-model="value1" :format="curFormat" display-format="YYYY-MM-DD-HH-mm-ss" mode="dateSelector"  @change="handleChange" @confirm="handleConfirm"
			@cancel="handleCancel" start-date="2021-1-1" :end-date="endTime">
		</lb-picker>
		<!-- end -->
		<!-- 血压选择 -->
		<!-- <lb-picker ref="picker2" v-model="value2" mode="multiSelector" :list="list2" :level="2"
			:dataset="{ name: 'label2' }" @change="handleChange" @confirm="handleConfirm" @cancel="handleCancel">
		</lb-picker> -->
		<lb-picker ref="picker2" v-model="value2" mode="unlinkedSelector" :list="list2" @change="handleChange"
			@confirm="handleConfirm2" @cancel="handleCancel" zIndex="1000">
		</lb-picker>
		<!-- end -->
	</view>
</template>

<script>
	import lbPicker  from '@/pages/subpackage/components/lb-picker/index';
	import dayjs from '@/common/utils/day/day.js'

import uLine from "@/uview-ui/components/u-line/u-line.vue"

	export default {
		components: {
			lbPicker,
			uLine
		},
		data() {
			return {
				curFormat: 'YYYY-MM-DD HH:mm:ss',
				disabled:false,
				endTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
				indicId: '', //指标id
				label2:'请选择血压',
				value2: ['收缩压',120,'舒张压',80],
				value1: '',
				label: '请选择时间',
				list2: [
					['收缩压'],
					[],
					['舒张压'],
					[],
				]
			}
		},
		onLoad(e) {
			this.indicId = e.indicId;
			this.getRangeIndicator();
		},
		methods: {
			// 转换选择
			listFun(min,max,min2,max2){
				console.log(min,max,min2,max2,8000)
				let listArr = [],
					list2 = this.list2,
					sArr = [];
				for(let i = min;i<max;i++){
					sArr.push(i)
				}
				for (let i = min2; i < max2; i++) {
					listArr.push(i)
				}
				this.$set(this.list2,1,sArr)
				this.$set(this.list2,3,listArr)
			},
			//测量指标范围列表
			getRangeIndicator(){
				this.$api.get(global.apiUrls.getRangeIndicator, {})
					.then(res => {
						let result = res.data.result,
						range;
						for(let i =0;i<result.length;i++){
							if('BLOOD_PRESSURE' == result[i].itemCode){//血压
								range = result[i].valueExpression.range
							}
						}
						let gangIdex = range.indexOf("|") //|的索引
						if (range.startsWith('|', gangIdex)) {
							let qgArr = range.split('|'),
								xsList = []
							console.log(qgArr, 490000000000)
							for (let i = 0; i < qgArr.length; i++) { //执行3次
								qgArr[i] = qgArr[i].replace(".", "-");
								qgArr[i] = qgArr[i].split("-");
								console.log(qgArr, 4944)
								for (let j = 0; j < qgArr[i].length; j++) {
									if (j == 0 || j == 2) {
										qgArr[i][j] = qgArr[i][j].replace('(', '') * 1;
										xsList.push(qgArr[i][j])
									} else {
										qgArr[i][j] = parseFloat(qgArr[i][j])
										xsList.push(qgArr[i][j])
									}
								}
							}
							this.listFun(xsList[0],xsList[1],xsList[2],xsList[3])
							console.log(xsList,12222)
							}

					})
			},
			// 血压
			handleConfirm2(e){
				console.log(e)
				this.label2 = e.item[0]+':'+e.item[1]+e.item[2]+':'+e.item[3]
			},
			// 血压
			addRecord(blood, time) {
				this.disabled = true
				let healthData = {
					systolicPre:  blood[1],
					diastolicPre: blood[3],
				}
				let _this = this,
					fdata = {
						deviceType:'BLOOD_PRESSURE',
						recordTime: dayjs(time).format('YYYY-MM-DD'),
						createTime:time,
						inputType:1,//1手动 2设备
						userId: uni.getStorageSync(global.IS_DEV?'USER_DATA_BSY_T':'USER_DATA_BSY_Z').userId,
						mac:'',//设备mac
						deviceId:'',//设备id
						healthDataId:'',
						healthData:JSON.stringify(healthData),
					};
				this.$api.post(global.apiUrls.addRecord, fdata).then(res => {
					if (res.data.code == 1000) {
						_this.label2 = '请选择血压'
						_this.value1=''
						uni.showToast({
							title: res.data.message,
							duration: 2000
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							});
						}, 2000);
					} else {
						uni.showToast({
							title: res.data.message,
							icon: "none",
							duration: 2000
						});
					}
					_this.disabled = false
				}).catch(err => {
					_this.disabled = false
				})
			},

			// userBp(blood, time) {
			// 	let _this = this,
			// 		fdata = {
			// 			recordTime: time,
			// 			diastolicPre: blood[3],
			// 			systolicPre: blood[1],
			// 			userId: uni.getStorageSync(global.IS_DEV?'USER_DATA_BSY_T':'USER_DATA_BSY_Z').userId,
			// 			indicId: this.indicId
			// 		};
			// 	this.$api.post(global.apiUrls.userBp, fdata).then(res => {
			// 		if (res.data.code == 1000) {
			// 			uni.showToast({
			// 				title: res.data.message,
			// 				duration: 2000
			// 			});
			// 			setTimeout(() => {
			// 				uni.navigateBack({
			// 					delta: 1
			// 				});
			// 			}, 2000);
			// 		} else {
			// 			uni.showToast({
			// 				title: res.data.message,
			// 				icon: "none",
			// 				duration: 2000
			// 			});
			// 		}
			// 	}).catch(err => {})
			// },
			handleTap(name) {
				this.$refs[name].show()
			},
			handleChange(e) {
				console.log('change::', e)
			},
			handleConfirm(e) {
				console.log(e, 123)
			},
			handleCancel(e) {
				console.log('cancel::', e)
			},
		}
	}
</script>

<style lang="scss">
	@import "./record.scss"
</style>
